package spring.boot.admin.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.boot.admin.mapper.SysRoleMapper;
import spring.boot.admin.model.Pages;
import spring.boot.admin.model.SysMenu;
import spring.boot.admin.model.SysRole;

import java.util.List;
import java.util.Map;

/**
 * 如果整个service都是使用的master数据源，可以放在类上面
 */
@Service
@DS("master")
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRole> {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 分页查询角色
     */
    public IPage<SysRole> pageList(Integer page, Integer rows) {
        Pages<SysRole> sysRolePage = new Pages<>(page, rows);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        return page(sysRolePage, wrapper);
    }


    /**
     * 查询所有有效的角色列表
     */
    public List<Map> selectUserRoleList() {
        return baseMapper.selectUserRoleList();
    }


    /**
     * 根据主键删除角色
     */
    public String deleteById(Long id) {
        //根据角色id查询 用户角色 关联关系数量
        int userRoleNum = sysUserRoleService.countByRoleId(id);
        if (userRoleNum > 0) {
            return "2";
        } else {
            //根据角色id查询角色菜单关联关系的数量
            int roleMenuNum = sysRoleMenuService.countByRoleId(id);
            if (roleMenuNum > 0) {
                return "3";
            } else {
                removeById(id);
                return "1";
            }
        }
    }

    /**
     * 新增角色
     */
    public String insert(String name, Integer enable) {
        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        sysRole.setEnable(enable);
        save(sysRole);
        return "1";
    }


    /**
     * 根据主键修改角色
     */
    public String updateRoleById(Long id, String name, Integer enable) {
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setName(name);
        sysRole.setEnable(enable);
        updateById(sysRole);
        return "1";
    }


    /**
     * 查询所有菜单tree，返回与角色的关联关系
     */
    public List<SysMenu> roleMenu(Long roleId) {
        List<SysMenu> oneList = sysMenuService.selectSimpleMenuByPid(0L);
        for (int i = 0; i < oneList.size(); i++) {
            Long oneId = oneList.get(i).getId();
            List<SysMenu> twoList = sysMenuService.selectSimpleMenuByPid(oneId);
            if (twoList == null || twoList.size() <= 0) {
                //没有二级子菜单
                oneList.get(i).setState("open");
                oneList.get(i).setChecked(sysRoleMenuService.hasRoleMenu(roleId, oneId));
            } else {
                //有二级子菜单
                oneList.get(i).setState("closed");
                for (int j = 0; j < twoList.size(); j++) {
                    Long twoId = twoList.get(j).getId();
                    List<SysMenu> threeList = sysMenuService.selectSimpleMenuByPid(twoId);
                    if (threeList == null || threeList.size() <= 0) {
                        //没有三级子菜单
                        twoList.get(j).setState("open");
                        twoList.get(j).setChecked(sysRoleMenuService.hasRoleMenu(roleId, twoId));
                    } else {
                        //有三级子菜单
                        twoList.get(j).setState("closed");
                        for (int k = 0; k < threeList.size(); k++) {
                            Long threeId = threeList.get(k).getId();
                            threeList.get(k).setState("open");
                            threeList.get(k).setChecked(sysRoleMenuService.hasRoleMenu(roleId, threeId));
                        }
                        twoList.get(j).setChildren(threeList);
                    }
                }
                oneList.get(i).setChildren(twoList);
            }
        }
        return oneList;
    }
}
